package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.EmployeeDao;
import cn.usian.exception.NameException;
import cn.usian.pojo.Employee;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
@Transactional//事物支持
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeDao employeeDao;

    //登录界面
    @Override
    public R<Employee> login(Employee employee) {
        //创建条件对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //1.根据用户查询用户信息是否存在
        Employee one = employeeDao.selectOne(queryWrapper);
        if (one==null){
            return R.error("用户不存在");
        }
        //2.如用户已经存在，判断密码 把用户输入的密码通过md5加密后跟数据库中的密码比较
        if (!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(one.getPassword())){
            return R.error("密码不正确");
        }
        //3.如果用户的账号密码都正确，判断用户的状态
        if (one.getStatus()!=1){
            return R.error("账号冻结");
        }
        return R.success(one);
    }

    //分页
    @Override
    public R findPage(int page, int pageSize, String name) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null, Employee::getName,name);
        //创建page对象
        Page<Employee>pageInfo=new Page<>(page,pageSize);
        //调用selectpage方法进行分页
        //自定义过滤器
        Page<Employee>selectPage=employeeDao.selectPage(pageInfo,queryWrapper);
        return R.success(selectPage);
    }

    //添加
    @Override
    public void save(Employee employee) {
        //判断当前用户是否存在
        //1.检查用户输入的用户是否在==>根据用户来查询用户信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //执行查询
        Employee selectOne=employeeDao.selectOne(queryWrapper);
        if (selectOne!=null){
            throw new NameException(employee.getUsername()); //throw触发异常
        }
        //保存用户的创建时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()).toString());
        employeeDao.insert(employee);
    }

    @Override
    public void updateEmp(Employee employee) {
       /* LambdaUpdateWrapper<Employee>lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Employee::getStatus,employee.getStatus());
        lambdaUpdateWrapper.set(Employee::getId,employee.getId());*/
        employeeDao.updateById(employee);
        R.success("修改成功");
    }

    @Override
    public R findById(Long id) {
        return R.success(employeeDao.selectById(id));
    }
}
